package la.bbs558.controller.admin;

import cn.dev33.satoken.annotation.SaCheckRole;
import la.bbs558.aop.valid.Check;
import la.bbs558.aop.valid.Valid;
import la.bbs558.controller.base.Result;
import la.bbs558.entity.HotWord;
import la.bbs558.entity.params.HotWordSearchPage;
import la.bbs558.satoken.StpAdminUtil;
import la.bbs558.service.HotWordService;
import la.bbs558.util.Strings;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * @author: Shanks
 * @date: 2023/12/15 16:59
 * @desc:
 */
@RestController
@RequestMapping("/admin/hotWord")
@RequiredArgsConstructor
@SaCheckRole(value = { "admin" },type = StpAdminUtil.TYPE)
@Check
public class HotWordManageController {

    private final HotWordService hotWordService;

    @GetMapping("list")
    public Result<?> list(HotWordSearchPage searchPage) {
        hotWordService.lambdaQuery()
                .eq(Strings.isNotEmpty(searchPage.getName()), HotWord::getName, searchPage.getName())
                .page(searchPage);
        return Result.success(searchPage);
    }

    @PostMapping("add")
    public Result<?> add(@RequestBody @Valid HotWord hotWord) {
        hotWordService.save(hotWord);
        return Result.success();
    }

    @PostMapping("update")
    public Result<?> update(@RequestBody @Valid HotWord hotWord) {
        hotWordService.updateById(hotWord);
        return Result.success();
    }

    @PostMapping("delete")
    public Result<?> delete(Long id) {
        hotWordService.removeById(id);
        return Result.success();
    }

}
